﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Category;
using DataObject.Product;

public partial class Controls_Product_TopProductByCategory : System.Web.UI.UserControl
{
    public int CategoryId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptProduct.DataSource = ProductController.Product_TopProductByCategory(CategoryId, SettingValues.PageSize_4);
            rptProduct.DataBind();
        }
    }
    
    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var categoryId  = ConvertUtility.ToInt32(((HiddenField) e.Item.FindControl("hfCategoryId")).Value);
            var productId   = ConvertUtility.ToInt32(((HiddenField) e.Item.FindControl("hfProductId")).Value);
            var title       = ((HiddenField) e.Item.FindControl("hfTitle")).Value;
            
            var ltTitle     = (Literal) e.Item.FindControl("ltTitle");
            var ltImage     = (Literal) e.Item.FindControl("ltImage");

            var categoryName = CategoryController.GetCategoryName(categoryId);
            var pHref = UrlHelpers.ProductDetail(categoryName, ConvertUtility.ToInt32(productId), title);

            var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=159&Fix=list", Constants.ThumbImage, productId, (int)CategoryType.Product);

            ltTitle.Text    = string.Format("<a href=\"{0}\" class=\"title colr\">{1}</a>", pHref, UnicodeUtility.CutStrExper(title, 10));
            ltImage.Text    = string.Format("<center><a href=\"{0}\" class=\"thumb\"><img src=\"{1}\" alt=\"\" /></a></center>", pHref, imgHref);
        }
    }    
}